<?php
//+----------------------------------------------------------------------
//| ThinkPHP [ WE CAN DO IT JUST THINK IT ]
//+----------------------------------------------------------------------
//| Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
//+----------------------------------------------------------------------
//| Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
//+----------------------------------------------------------------------
//| Author: liu21st <liu21st@gmail.com>
//+----------------------------------------------------------------------
//$Id: App.class.php 2792 2012-03-02 03:36:36Z liu21st $
/**
 +------------------------------------------------------------------------------
 * ThinkPHP 应用程序类 执行应用过程管理
 * 可以在模式扩展中重新定义 但是必须具有Run方法接口
 +------------------------------------------------------------------------------
 * @category   Think
 * @package  Think
 * @subpackage  Core
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id: App.class.php 2792 2012-03-02 03:36:36Z liu21st $
 * @last modify 2012-12-04 17:04:58 by mashanling
 +------------------------------------------------------------------------------
 */
class App {

    /**
     * 执行应用程序
     *
     * @last modify 2012-12-04 17:08:26 by mashanling
     *
     * @access static,public
     *
     * @return mixed
     */
    static public function exec() {

        if (!preg_match('/^[A-Za-z_0-9]+$/', MODULE_NAME)) {//安全检测
            $module = false;
        }
        else {//创建Action控制器实例
            $module = A(MODULE_NAME);
        }

        if (!$module) {
            //hack方式定义扩展模块 返回Action对象
            if (function_exists('__hack_module') && !is_object($module = __hack_module())) {
                return;
            }
            else {
                $module = A('Empty');//是否定义Empty模块

                if (!$module) {
                    $msg = L('_MODULE_NOT_EXIST_') . MODULE_NAME;

                    if (APP_DEBUG) {//模块不存在 抛出异常
                        throw new Exception($msg);
                    }
                    else {
                        trigger_error($msg, E_USER_ERROR);
                        send_http_status(404);
                        exit();
                    }
                }
            }
        }

        $action = ACTION_NAME;//获取当前操作名

        tag('action_name', $action);//获取操作方法名标签

        method_exists($module, $c = '_before_' . $action) && call_user_func(array(&$module, $c));//执行前置操作
        call_user_func(array(&$module, $action ));//执行当前操作
        method_exists($module, $c = '_after_' . $action) && call_user_func(array(&$module, $c));//执行后缀操作

        return;
    }//end exec

    /**
     *
     * 应用程序初始化
     *
     * @last modify 2012-12-04 17:15:00 by mashanling
     *
     * @access static,public
     *
     * @return void 无返回值
     */
    static public function init() {
        date_default_timezone_set(C('DEFAULT_TIMEZONE'));//设置系统时区

        //定义一些常量 by mashanling on 2012-09-11 09:47:20
        define('APP_NOW_TIME' , gmtime()); //当前本地unix时间戳

        load_ext_file();//加载动态项目公共文件和配置 conf/db.php
        Dispatcher::dispatch();//URL调度

        //获取模板主题名称
        $template_set = C('DEFAULT_THEME');

        if (C('TMPL_DETECT_THEME')) {//自动侦测模板主题
            $t = C('VAR_TEMPLATE');

            if (isset($_GET[$t])) {
                $template_set = $_GET[$t];
            }
            elseif ($c = cookie('think_template')) {
                $template_set = $c;
            }

            !is_dir(TMPL_PATH . $template_set) && $template_set = C('DEFAULT_THEME');//主题不存在时仍改回使用默认主题

            cookie('think_template', $template_set);
        }

        //模板相关目录常量
        define('THEME_NAME', $template_set); //当前模板主题名称
        define('THEME_PATH', TMPL_PATH . (THEME_NAME ? THEME_NAME . '/' : ''));
        define('APP_TMPL_PATH', __ROOT__ . '/' . APP_NAME . (APP_NAME ? '/' : '') . basename(TMPL_PATH) . '/' . (THEME_NAME ? THEME_NAME . '/' : ''));
        C('TEMPLATE_NAME', THEME_PATH . MODULE_NAME . '/' . ACTION_NAME . C('TMPL_TEMPLATE_SUFFIX'));
        C('CACHE_PATH', CACHE_PATH);

        //增加通用语言包 by mashanling on 2012-07-04 09:56:01
        is_file($lang_file = LANG_PATH . C('DEFAULT_LANG') . '/_common.php') && L(include ($lang_file));
        is_file($lang_file = LANG_PATH . C('DEFAULT_LANG') . '/' . strtolower(MODULE_NAME) . '.php') && L(include ($lang_file));
    }//end init

    /**
     *
     * 运行应用实例 入口文件使用的快捷方法
     *
     * @last modify 2012-12-04 17:15:45 by mashanling
     *
     * @access static,public
     *
     * @return void 无返回值
     */
    static public function run() {

        if (!MAGIC_QUOTES_GPC) {
            if (!empty($_GET)) {
                $_GET = addslashes_deep($_GET);
            }
            if (!empty($_POST)) {
                $_POST = addslashes_deep($_POST);
            }
            if (!empty($_COOKIE)) {
                $_COOKIE = addslashes_deep($_COOKIE);
            }
        }

        self::init();
        session(C('SESSION_OPTIONS'));//Session初始化

        G('initTime');//记录应用初始化时间

        self::exec();
        Log::save();//保存日志记录
    }
}